#!/usr/bin/env python3

import os
import subprocess
import gi

gi.require_version('Gtk', '3.0')
gi.require_version('AyatanaAppIndicator3', '0.1')

from gi.repository import Gtk, Gdk, AyatanaAppIndicator3

ICON = "/usr/share/simple-notifier/icons/up2date.png"


# ---------------------------------------------------------
# Menu Actions
# ---------------------------------------------------------

def script1(_):
    subprocess.Popen(["/usr/share/simple-notifier/scripts/pswd-gui"])

def script2(_):
    subprocess.Popen(["/usr/share/simple-notifier/scripts/history-cli"])

def script3(_):
    subprocess.Popen(["/usr/share/simple-notifier/scripts/pswd-refresh"])

def script4(_):
    subprocess.Popen(["/usr/share/simple-notifier/scripts/settings"])

def script5(_):
    subprocess.Popen(
        ["/usr/share/simple-notifier/about/Simple Update Notifier"]
    )


# ---------------------------------------------------------
# Build Menu
# ---------------------------------------------------------

def build_menu():
    menu = Gtk.Menu()

    items = [
        ("📦 DNF Manager", script1),
        ("⏳ History", script2),
        ("♻️ Refresh", script3),
        ("⚙ Setting", script4),
        ("💬 About", script5),
    ]

    for label, callback in items:
        item = Gtk.MenuItem.new_with_label(label)
        item.connect("activate", callback)
        menu.append(item)

    menu.show_all()
    return menu


menu = build_menu()

# ---------------------------------------------------------
# Detect Session Type
# ---------------------------------------------------------

is_wayland = (
    os.environ.get("WAYLAND_DISPLAY") is not None
    or os.environ.get("XDG_SESSION_TYPE") == "wayland"
)

desktop = os.environ.get("XDG_CURRENT_DESKTOP", "")

print("Desktop:", desktop)
print("Wayland:", is_wayland)

# ---------------------------------------------------------
# Wayland → AppIndicator
# ---------------------------------------------------------

if is_wayland:

    indicator = AyatanaAppIndicator3.Indicator.new(
        "simple-notifier",
        ICON,
        AyatanaAppIndicator3.IndicatorCategory.APPLICATION_STATUS
    )

    indicator.set_status(
        AyatanaAppIndicator3.IndicatorStatus.ACTIVE
    )

    indicator.set_menu(menu)

# ---------------------------------------------------------
# X11 → Gtk.StatusIcon
# ---------------------------------------------------------

else:

    def on_status_icon_click(icon, button, time):
        menu.popup(
            None,
            None,
            Gtk.StatusIcon.position_menu,
            icon,
            button,
            time
        )

    status_icon = Gtk.StatusIcon()
    status_icon.set_from_file(ICON)
    status_icon.set_visible(True)

    status_icon.connect(
        "popup-menu",
        on_status_icon_click
    )

    status_icon.connect(
        "activate",
        lambda icon: menu.popup(
            None,
            None,
            Gtk.StatusIcon.position_menu,
            icon,
            1,
            Gtk.get_current_event_time()
        )
    )

Gtk.main()
